package sg.edu.nus.iss.vmcs.store;

import java.util.Observable;

/*
 * Copyright 2011 ISS.
 * The contents contained in this document may not be reproduced in any
 * form or by any means, without the written permission of ISS, other
 * than for the purpose for which it has been supplied.
 *
 */

/**
 * This entity object  implements a generic storage item class. It performs 
 * actions like; returning content (Store Item identification), setting 
 * quantity, returning quantity, increment quantity, decrement (release) 
 * quantity . 
 */

public class StoreItem extends Observable implements Cloneable {

	private StoreObject content;
	private int quantity;
        
/**
 * This constructor creates a new instance of the Store Item. It 
 * receives the quantity of the Store Item and a reference to the Store 
 * Object it contains. 
 * @param content the name of the StoreObject that will go into the 
 * Store Item.
 * @param quantity the quatity of StoreObject that will be stored in
 * the StoreItem.
 */
	public StoreItem(StoreObject content, int quantity) {
		this.content = content;
		this.quantity = quantity;
		
	}

/**
 * Returns the object type that is stored in the Store Item. 
 * @return returns the StoreObject in the Store Item.
 */
	public StoreObject getContent() {
		return content;
	}

/**
 * This operation sets the Store Object held by the Store Item. 
 * @param ct the StoreObject in the Store Item.
 */
	public void setContent(StoreObject ct) {
		content = ct;
	}

/**
 * This operation sets the total number of the store item held. 
 * @param quantity the quantity of StoreObject held in the Store Item.
 */
	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

/**
 * Returns the total number of the store item held. 
 * @return returns the quantity of StoreObject in the StoreItem.
 */       
	public int getQuantity() {
		return quantity;
	}

/**
 * This operation increments the item quantity by one, and returns 
 * whether successful.
 */
	public void store() {
		quantity++;
	}
	
/**
 * This operation decrements the item quantity by one, and returns 
 * whether successful. 
 */
	public void decrement() {
		quantity--;
		if (quantity < 0)
			quantity = 0;
		
		//CALLING THE NOTIFY OBSERVERS
		setChanged();
		notifyObservers(this);
	}

/**
 * This operation increments the item quantity by one, and returns 
 * whether successful. 
 */
	public void increment() {
		quantity++;
		//CALLING THE NOTIFY OBSERVERS
		setChanged();
		notifyObservers(this);
	}
	
	/**
	 * This operation clone the StoreItem ,which is required for creating deep copy of
	 * StoreItem arrays in order to save the copy into the Memento. 
	 */
	
	public StoreItem clone() {
		
		try {
			return (StoreItem)super.clone();
		} catch (CloneNotSupportedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}